// pages/search/search.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    history: [],
    key: "",
    showActionSheet: false,
    tips: "确认清空搜索历史吗？",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      history: this.getHistory()
    })
  },

  getHistory(){
    let data=wx.getStorageSync("history") || "[]";
    return JSON.parse(data);
  },
  setHistory(){
    let data=this.getHistory();
    if(data.indexOf(this.data.key)<0){
      data.push(this.data.key);
      wx.setStorageSync("history",JSON.stringify(data));
    }
  },
  keyTap(key){
    wx.navigateTo({
      url:"/pages/list/list?searchKey="+key
    })
  },
  gotoSearch(){
    if(this.data.key==""){
      wx.util.toast('关键词不能为空');
      return;
    }
    this.setHistory();
    wx.navigateTo({
      url:"/pages/list/list?searchKey="+this.data.key
    })
  },
  back: function() {
    wx.navigateBack();
  },
  cleanKey: function() {
    this.setData({
      key: ''
    })
  },
  closeActionSheet: function() {
    this.setData({
      showActionSheet: false
    })
  },
  openActionSheet: function() {
    this.setData({
      showActionSheet: true
    })
  },
  itemClick: function(e) {
    let index = e.detail.index;
    if (index == 0) {
      this.setData({
        showActionSheet: false,
        history: []
      })
      wx.removeStorageSync("history");
    }
  },
  trim: function(value) {
		return value.replace(/(^\s*)|(\s*$)/g, "");
	},
  inputKey: function(e) {
    this.setData({
      key: this.trim(e.detail.value)
    })
  }
})